/*
 * @Author       : Deusit && 505097558@qq.com
 * @Date         : 2023-06-05 23:07:49
 * @LastEditors  : Deusit && 505097558@qq.com
 * @LastEditTime : 2023-07-29 16:56:03
 * @Description  : file content
 * @FilePath     : program.cpp
 * Copyright (c) 2023 by Deusit email: 505097558@qq.com, All Rights Reserved.
 */

#include <deusitWifi.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define programVersion "1.0.0"
unsigned long lastMsg = 0;

DEUSIT_WIFI deusitWifi(/*设备名*/ "测试001号", /*网络标识符*/ "测试001号", /*热点名*/ "Test中文");

U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ D2, /* data=*/ D1, /* reset=*/ U8X8_PIN_NONE);

void program_start()
{
    /*deviceName = "测试001号";   //设备名
    hostName = "测试001号";     //网络标识符
    APName = "Test中文";        //热点名*/

    deusitWifi.begin();

    /*webServer.on("/hello", [](){
        webServer.send(200, "text/plain", "Hello, world!");
    });*/

    Serial.print("Program Version:");
    Serial.println(programVersion);

    u8g2.begin();
    u8g2.enableUTF8Print();
    
    u8g2.setFont(u8g2_font_wqy16_t_gb2312);
    u8g2.setFontDirection(0);
}

void info()
{
    Serial.print("\nProgram Version:");
    Serial.println(programVersion);
}

void program_loop()
{
    //wifiHeart();
    //mqttPub();
    
    //String test = readConfig();
    //if(test.length()>0) Serial.println(test);

}

void mqttSub(char* topic, byte* payload, unsigned int length) {
    String message = "";
    for (unsigned int i = 0; i < length; i++) {
        message += (char)payload[i];
    }
    Serial.println("MQTT 接收消息 ["+String(topic)+"]:"+message);
    u8g2.firstPage();
    do {
        u8g2.setCursor(0, 15);
        u8g2.print(topic);
        u8g2.setCursor(0, 40);
        u8g2.drawUTF8(0, 40, message.c_str());
    } while ( u8g2.nextPage() );
}

void mqttPub(){
    /*if(mqttClient.connected()){
        unsigned long now = millis();
        if (now - lastMsg > 2000) {
            lastMsg = now;
            String str = readConfig();
            Serial.println("发布: "+str);
            mqttClient.publish("TEST", str.c_str());
        }
    }*/
}